package com.iman.locker.view.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.iman.dao.User;
import com.iman.locker.MyApp;
import com.iman.locker.R;
import com.iman.locker.view.LockActivity;
import com.iman.locker.view.widget.PinNumericKeyboard;


/**
 * Pin密码验证页面
 * Created by esirong on 2015/11/10.
 */
public class PinAuthenticationFragment extends AuthenticationFragment {

    private AuthorizeListener mListener;
    private String firstPassword;
    private boolean reSetFirstPassword = true;
    private PinNumericKeyboard numericKeyboard;
    private boolean isNeedSetPassword = false;
    private static final String PASS_STATUS = "editable";
    TextView passwordTips;

    public static PinAuthenticationFragment instance(boolean isPasswordEdited) {
        PinAuthenticationFragment instance = new PinAuthenticationFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(PASS_STATUS, isPasswordEdited);
        instance.setArguments(bundle);
        return instance;
    }

    /**
     * 重置锁
     */
    public void setResetPassword() {
        isNeedSetPassword = true;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            mListener = (AuthorizeListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + "must implement AuthorizeListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_validation_pin, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            isNeedSetPassword = bundle.getBoolean(PASS_STATUS);
        }
        //检查是不是没有设置密码状态
        if (isNeedEditPassword()) {
            TextView passwordTips = (TextView) view.findViewById(R.id.password_title);
            passwordTips.setText(R.string.tips_setting_pin_password);
        }
        passwordTips = (TextView) view.findViewById(R.id.password_tips);

        //显示提示语，要求设置密码
        numericKeyboard = (PinNumericKeyboard) view.findViewById(R.id.numeric_keyboard);
        numericKeyboard.setCallBack(new PinNumericKeyboard.CallBack() {
            @Override
            public void onInput(String password) {
                doAuthorize(password);
            }
        });

    }
    public void setPasswordStatus(boolean isNeedSetPassword){
        this.isNeedSetPassword = isNeedSetPassword;
    }
    private boolean isNeedEditPassword() {
        return !isSetPinPassword() || isNeedSetPassword;
    }

    private boolean isSetPinPassword() {
        User user = MyApp.getInstance().getUser();
        return !(user == null || TextUtils.isEmpty(user.getPasswordPin()));
    }

    //鉴权
    private boolean doAuthorize(String password) {

        if (isNeedEditPassword()) {
            initDrawAuthorize(password);
            return false;
        } else {
            //密码验证
            User user = MyApp.getInstance().getUser();
            if (user.getPasswordPin().equals(password)) {
                mListener.onAuthorizeDone(true);
                user.setLockType(LockActivity.TAB_PIN_TO_UNLOCK);
                MyApp.getInstance().setUser(user);
            } else {
                mListener.onAuthorizeDone(false);
                passwordTips.setText(R.string.error_wrong_password);
                numericKeyboard.startAnimate();
            }
        }

        return false;
    }

    /**
     * 初始化手势密码
     *
     * @param password 密码
     */
    private void initDrawAuthorize(String password) {
        if (password.length() < 4 || password.length() > 12) {
            passwordTips.setText(R.string.msg_dot_is_less_than_4);
            return;
        }
        if (reSetFirstPassword) {
            firstPassword = password;
            reSetFirstPassword = false;
            passwordTips.setText(R.string.confirm_password);

        } else {
            if (firstPassword.equals(password)) {
                User user = MyApp.getInstance().getUser();
                user.setPasswordPin(password);
                user.setLockType(LockActivity.TAB_PIN_TO_UNLOCK);
                MyApp.getInstance().setUser(user);
                mListener.onAuthorizeDone(true);
            } else {
                passwordTips.setText(R.string.error_confirm_no_match);
                reSetFirstPassword = true;
            }
        }

    }

}
